#!/bin/bash
# 设置mysql的登录用户名和密码(根据实际情况填写)
mysqlUser="root"
mysqlPassword="123456"
# mysql_host="localhost"
# mysql_port="3306"
# mysql_charset="utf8mb4"
containerID='326d94925494'
#dbname=''

# 备份文件存放地址(根据实际情况填写)
#backup_dir='/usr/local'
backup_dir=~/docker-bak/
# 判断目录是不是已经存在，如果不存在则创建
if [ ! -d $backup_dir ]; then
  mkdir -p $backup_dir
fi
#===================================
# 是否删除过期数据
expire_backup_delete="ON"
expire_days=7
backup_time=`date +%Y%m%d%H%M`
welcome_msg="Welcome to use MySQL backup tools!"
#===========================================
# 备份指定数据库中数据(此处假设数据库是mysql_backup_test)
#docker exec $containerIDmysqldump -u$mysqlUser -p$mysqlPassword -B $dbname > $backup_dir/$dbname-$backup_time.sql

#备份全部数据库
docker exec ${containerID} mysqldump -u${mysqlUser} -p${mysqlPassword} -A > ${backup_dir}/alldb-${backup_time}.sql
#==================================================
#删除过期数据
if [ "$expire_backup_delete" == "ON" -a  "$backup_dir" != "" ];then
        `find $backup_dir/ -type f -mtime +$expire_days | xargs rm -rf`
        echo "Expired backup data delete complete!"
fi